package router

import (
	"schedulingcfg/api"
	"schedulingcfg/config"
	"schedulingcfg/dao/pool"
	"schedulingcfg/utils"

	"github.com/bwmarrin/snowflake"
	"github.com/gin-gonic/gin"
)

func NewRouter() *gin.Engine {
	cfg := config.GetConfig()
	node, _ := snowflake.NewNode(cfg.Server.WorkerId)
	server := gin.Default()

	group := server.Group("/", gin.BasicAuth(gin.Accounts{
		"admin": "admin123",
		"sirui": "sirui",
	}))
	handler := api.NewBaseApi(api.WithGorm(pool.GetDB()), api.WithRouterGroup(group), api.WithSnowflake(node))
	api.NewPermissionsApi(handler)
	//代码构建器
	api.NewCfDatasourcesApi(handler)
	api.NewCfFlowsApi(handler)
	api.NewNacosServicesApi(handler)
	api.NewSignKeysApi(handler)
	api.NewNacosConfigsApi(handler)
	return server
}

func Recovery(c *gin.Context) {
	defer func() {
		if r := recover(); r != nil {
			utils.FailResult(c, "系统内部错误")
		}
	}()
	c.Next()
}
